***********************************
***** Regaining Control?   ********
***** Omer Solodoch       *********
***** Tel Aviv University *********
***** Replication code  ***********
***********************************

local directory "C:\Users\LENOVO\Dropbox\soloD\Projects\IO control"
cd "`directory'"



use data/EBdataIO.dta

**** Figure 2: balance test
logit cutoff_border11D age i.Woman i.educat i.d25 i.NUTS_2_SE  if cutoff_border11 <= 0 & cutoff_border11 >= -1
estimates store EBd01
logit cutoff_border11D age i.Woman i.educat i.d25 i.NUTS_2_SE if cutoff_border11 <= 2 & cutoff_border11 >= -2 
estimates store EBd02
logit cutoff_border11D age i.Woman i.educat i.d25 i.NUTS_2_SE if cutoff_border11 <= 3 & cutoff_border11 >= -3 
estimates store EBd03
logit cutoff_border11D age i.Woman i.educat i.d25 i.NUTS_2_SE 
estimates store EBd06

coefplot (EBd06), bylabel(All [-4,+6] days) ///
|| (EBd03), bylabel(±3 days) ///
|| (EBd02), bylabel(±2 days) ///
|| (EBd01), bylabel(±1 days) ///
||, xline(0, lwidth(thin) lpattern(dash)) ///
omitted baselevels headings(0.Woman = "Gender" 1.educat = "Education" 1.d25 = "Community type" 1.NUTS_2_SE = "NUTS2") drop(_cons) /// 
coeflabels(, labsize(vsmall)) ci(95 90)  ///
graphregion(color(white) lcolor(black)) ///
plotregion(color(gs15) lcolor(black)) msize(vsmall) mcolor(black) msymbol(C)


*************************************
* Data for Figure 3:
table cutoff_border11, c(mean anti_nonEUmig_d semean anti_nonEUmig_d n anti_nonEUmig_d) by(Sweden)
mean anti_nonEUmig_d if Sweden == 1, over(cutoff_border11D)

*************************************
************ data for Figure 4: Policy effects:
foreach var in mig_contribute_d pro_nonEUmig_d pro_EUmig_d helpref1 trust_govt trust_parties trust_parliament democ_satisfied {
reg `var' i.cutoff_border11D age i.Woman i.education i.d25 i.NUTS_2_SE if Sweden == 1 
}


************ Table 1: RD estimates in narrower time windows
** divide Diff. in means [r(obs_stat)] by [S.D. of outcome] for [Left of c] to obtain the effect in S.D. terms 

* Cols 1-3
rdrandinf qd11_3_n cutoff_border11 if Sweden == 1, wl(-1) wr(1) 
display r(obs_stat)/0.836
rdrandinf qd11_3_n cutoff_border11 if Sweden == 1, wl(-2) wr(2) 
display r(obs_stat)/0.850
rdrandinf qd11_3_n cutoff_border11 if Sweden == 1, wl(-3) wr(3) 
display r(obs_stat)/0.835

* Cols 4-6
rdrandinf qb4_2_n cutoff_border11 if Sweden == 1, wl(-1) wr(1) 
display r(obs_stat)/0.930
rdrandinf qb4_2_n cutoff_border11 if Sweden == 1, wl(-2) wr(2) 
display r(obs_stat)/0.920
rdrandinf qb4_2_n cutoff_border11 if Sweden == 1, wl(-3) wr(3) 
display r(obs_stat)/0.895

* Cols 7-9
rdrandinf qb4_1_n cutoff_border11 if Sweden == 1, wl(-1) wr(1) 
display r(obs_stat)/0.814
rdrandinf qb4_1_n cutoff_border11 if Sweden == 1, wl(-2) wr(2) 
display r(obs_stat)/0.801
rdrandinf qb4_1_n cutoff_border11 if Sweden == 1, wl(-3) wr(3) 
display r(obs_stat)/0.773
**************************************************************

**** Effect Mechanisms: Table 3 Models 1-3:
reg anti_nonEUmig_d i.cutoff_border11D i.community i.education age Woman if Sweden == 1 & cutoff_border11 <= 3 & cutoff_border11 >= -3
reg anti_nonEUmig_d i.cutoff_border11D##South_SE i.community i.education age Woman if Sweden == 1 & cutoff_border11 <= 3 & cutoff_border11 >= -3
reg anti_nonEUmig_d i.cutoff_border11D##left_wing i.community i.education age Woman if Sweden == 1 & cutoff_border11 <= 3 & cutoff_border11 >= -3


**** Table 4: interaction effects +  the marginal effect of border controls on each country
foreach var in pro_nonEUmig_d mig_contribute_d democ_satisfied  trust_parliament    {
reg `var' cutoff_border11D##Sweden age i.Woman i.education i.d25  if (Sweden == 1 | Denmark==1) & cutoff_border11 <= 6 & cutoff_border11 >= -6
margins, dydx(cutoff_border11D) at (Sweden = 0)
margins, dydx(cutoff_border11D) at (Sweden = 1)
}



****************************************************
**** Online Appendix
*********** Table S3.1: balance test
logit cutoff_border11D age42 age43 age44 i.Woman educat2 educat3 educat4 educat5 i.d25 i.NUTS_2_SE  if cutoff_border11 <= 0 & cutoff_border11 >= -1
logit cutoff_border11D age42 age43 age44 i.Woman educat2 educat3 educat4 educat5 i.d25 i.NUTS_2_SE if cutoff_border11 <= 2 & cutoff_border11 >= -2 
logit cutoff_border11D age42 age43 age44 i.Woman educat2 educat3 educat4 educat5 i.d25 i.NUTS_2_SE if cutoff_border11 <= 3 & cutoff_border11 >= -3 
logit cutoff_border11D age42 age43 age44 i.Woman educat2 educat3 educat4 educat5 i.d25 i.NUTS_2_SE 


*********** Table S3.2: Placebo Units
qui reg anti_nonEUmig_d cutoff_border11D i.education i.community age i.Woman if COUNTRY16 == 1
*outreg2 using results/[].xls, alpha(0.001, 0.01, 0.05) keep(cutoff_border11D) dec(3) append label 
qui reg anti_nonEUmig_d cutoff_border11D i.education i.community age i.Woman i.country
*outreg2 using results/[].xls, alpha(0.001, 0.01, 0.05) keep(cutoff_border11D) dec(3) append label 

forvalue i = 1/30 {
qui reg anti_nonEUmig_d cutoff_border11D i.education i.community age i.Woman if COUNTRY`i' == 1
*outreg2 using results/[].xls, alpha(0.001, 0.01, 0.05) keep(cutoff_border11D) dec(3) append label 
}

qui reg anti_nonEUmig_d cutoff_border11D i.education i.community age i.Woman if COUNTRY16 == 1 & (cutoff_border11 <= 2 & cutoff_border11 >= -2)
*outreg2 using results/[].xls, alpha(0.001, 0.01, 0.05) keep(cutoff_border11D) dec(3) append label 
qui reg anti_nonEUmig_d cutoff_border11D i.education i.community age i.Woman i.country if cutoff_border11 <= 2 & cutoff_border11 >= -2
*outreg2 using results/[].xls, alpha(0.001, 0.01, 0.05) keep(cutoff_border11D) dec(3) append label 

forvalue i = 1/30 {
qui reg anti_nonEUmig_d cutoff_border11D i.education i.community age i.Woman if COUNTRY`i' == 1 & (cutoff_border11 <= 2 & cutoff_border11 >= -2)
*outreg2 using results/[].xls, alpha(0.001, 0.01, 0.05) keep(cutoff_border11D) dec(3) append label 
}

****************************************************
*********** Table S3.3: Placebo Outcomes
foreach var in mig_nocontribute_d qd6_7 qd6_8 qd7_1 qd7_7 qd7_9 qd5t {
reg `var' cutoff_border11D age i.Woman i.education i.d25 i.NUTS_2_SE if Sweden == 1 & (cutoff_border11 <= 2 & cutoff_border11 >= -2)
}

*********** Table S3.4: Placebo cutoffs
forvalue i = 8/15 {
rdrandinf qd11_3_n cutoff_border`i' if Sweden == 1 & cutoff_border`i' != 0, wl(-2) wr(2) 
}


*Data for Figure S3.1, left-panel:
table poli_5 if Sweden == 1 & cutoff_border11D==0, c(mean anti_nonEUmig_d semean anti_nonEUmig_d) 


*********** Table S3.5: alternative measures for left-wing identification (the progressive backlash hypothesis)
reg mig_nocontribute_d i.cutoff_border11D##helpref1 age i.Woman i.education i.d25 i.NUTS_2_SE if Sweden == 1
reg mig_nocontribute_d i.cutoff_border11D##qd8_9 age i.Woman i.education i.d25 i.NUTS_2_SE if Sweden == 1
reg mig_nocontribute_d i.cutoff_border11D##qd8_4 age i.Woman i.education i.d25 i.NUTS_2_SE if Sweden == 1
reg mig_nocontribute_d i.cutoff_border11D##qd8_2 age i.Woman i.education i.d25 i.NUTS_2_SE if Sweden == 1



****************************************************
******* Table S3.9: Causal mediation analysis:
reg anti_nonEUmig_d i.cutoff_border11D age i.Woman i.education i.d25 i.NUTS_2_SE if Sweden == 1 
reg trust_parliament i.cutoff_border11D age i.Woman i.education i.d25 i.NUTS_2_SE if Sweden == 1 
reg anti_nonEUmig_d i.cutoff_border11D trust_parliament age i.Woman i.education i.d25 i.NUTS_2_SE if Sweden == 1 



****************************************************
****** data for Figure 8 ***************************

clear
use data/EBlongdataIO.dta

table wave if country16==1, c(mean anti_nonEU_imm_D semean anti_nonEU_imm_D n anti_nonEU_imm_D) by(Bcontrols)

*******************************

clear
use data/GIPdata1IO.dta


***** Figure 5: Balance test
logit cutoff_EUTR18D i.age_3cat i.gender i.educ_school i.state if cutoff_EUTR18 <= 1 & cutoff_EUTR18 >= -1
estimates store GIPd01
logit cutoff_EUTR18D i.age_3cat i.gender i.educ_school i.state if cutoff_EUTR18 <= 3 & cutoff_EUTR18 >= -3
estimates store GIPd03
logit cutoff_EUTR18D i.age_3cat i.gender i.educ_school i.state if cutoff_EUTR18 <= 5 & cutoff_EUTR18 >= -5
estimates store GIPd05
logit cutoff_EUTR18D i.age_3cat i.gender i.educ_school i.state 
estimates store GIPd015

coefplot (GIPd015), bylabel(All [-17,+13] days) ///
|| (GIPd05), bylabel(±5 days) ///
|| (GIPd03), bylabel(±3 days) ///
|| (GIPd01), bylabel(±1 days) ///
||, xline(0, lwidth(thin) lpattern(dash)) ///
omitted baselevels headings(1.age_3cat = "Age" 1.gender = "Gender" 1.educ_school = "Education" 1.state = "State") drop(_cons) /// 
coeflabels(, labsize(vsmall)) ci(95 90) ///
graphregion(color(white) lcolor(black)) ///
plotregion(color(gs15) lcolor(black)) msize(vsmall) mcolor(black) msymbol(C)

***** data for Figure 6: Effects of The EU-Turkey Agreement on Public Attitudes and Placebo Outcomes
foreach var in security_refuge_D vote_AfD keep_admission overcome_crisis use_Facebook use_Instagram use_Linkedin use_Twitter {
reg `var' i.cutoff_EUTRD i.gender i.age_3cat educ_school i.state 
}

foreach var in security_refuge_D vote_AfD keep_admission overcome_crisis use_Facebook use_Instagram use_Linkedin use_Twitter {
reg `var' i.cutoff_EUTRD i.gender i.age_3cat educ_school i.state  if cutoff_EUTR18 <= 3 & cutoff_EUTR18 >= -3 
}

***** Table 2 - Columns 1-6**
foreach var in security_refuge_D vote_AfD  {
forvalue i = 1/3 {
rdrandinf `var' cutoff_EUTR, wl(-`i') wr(`i')   
}
}



****************************
clear
use data/GIPdata2IO.dta

***** data for Figure 7: 
table treatment_3, c(mean vote_AfD_0315 semean vote_AfD_0315 n vote_AfD_0315)
table treatment_3, c(mean vote_AfD_0915 semean vote_AfD_0915 n vote_AfD_0915)
table treatment_3, c(mean vote_AfD_0316 semean vote_AfD_0316 n vote_AfD_0316)
table treatment_3, c(mean vote_AfD_0916 semean vote_AfD_0916 n vote_AfD_0916)

****** Table 2 Model 7 - DID estimation
*** Column 7:
xtreg vote_AfD i.pol_treatment_3 i.wave, fe
**************

***** Table 3 - Columns 4-6**
*** Model 4: no interaction
reg security_refuge_D i.treatment_3 gender i.age_3cat i.educ_school i.state if wave == 3
*** Model 5: direct exposure
reg security_refuge_D i.treatment_3##i.state7 gender i.age_3cat i.educ_school i.state if wave == 3
*** Model 6: left-wing backlash
reg security_refuge_D i.treatment_3##i.left_wing gender i.age_3cat i.educ_school i.state if wave == 3

** data for Figure S3.1 right-panel:
recode poli_10 (1/3=1 "Left") (4/5=2 "Center-Left") (6=3 "Center") (7/8=4 "Center-Right") ///
(9/11=5 "Right"), gen(poli_5)
table poli_5 if cutoff_EUTR18D==0 & wave==3, c(mean security_refuge_D semean security_refuge_D n security_refuge_D)

***********************
clear
use data/GIPdata1IO.dta

*** table S3.6 (balance table)
logit cutoff_EUTR18D i.age_4cat i.gender i.educ_school i.state if cutoff_EUTR18 <= 1 & cutoff_EUTR18 >= -1
logit cutoff_EUTR18D i.age_4cat i.gender i.educ_school i.state if cutoff_EUTR18 <= 3 & cutoff_EUTR18 >= -3
logit cutoff_EUTR18D i.age_4cat i.gender i.educ_school i.state if cutoff_EUTR18 <= 5 & cutoff_EUTR18 >= -5
logit cutoff_EUTR18D i.age_4cat i.gender i.educ_school i.state 

*** table S3.7 (placebo outcomes, Germany)
foreach var in vote_AfD_0915 pol_interest criticize_others {
reg `var' cutoff_EUTRD i.age_4cat i.gender i.educ_school i.state 
reg `var' cutoff_EUTRD i.age_4cat i.gender i.educ_school i.state if cutoff_EUTR18 <= 3 & cutoff_EUTR18 >= -3
}


***********************
clear
use data/GLESdataIO.dta

******* data for Figure 9 **********
reg scared_fD i.wave female i.age_cat3 i.educ i.elect_dist 
reg economic_impact_bD i.wave female i.age_cat3 i.educ i.elect_dist 
reg reject_refugees_d i.wave female i.age_cat3 i.educ i.elect_dist 
reg asylum_kD i.wave female i.age_cat3 i.educ i.elect_dist 
reg VOTE9 i.wave female i.age_cat3 i.educ i.elect_dist 
reg mig_policy_pref_D i.wave female i.age_cat3 i.educ i.elect_dist 



******* Table S3.8 **********
reg scared_fD i.wave i.age_cat3 i.educ i.elect_dist if wave<=6 
reg scared_fD i.wave##on_border4 i.age_cat3 i.educ i.elect_dist if wave<=6 

reg reject_refugees_d i.wave i.age_cat3 i.educ i.elect_dist if wave<=6 
reg reject_refugees_d i.wave##on_border4 i.age_cat3 i.educ i.elect_dist if wave<=6 

reg asylum_kD i.wave i.age_cat3 i.educ i.elect_dist if wave<=6 
reg asylum_kD i.wave##on_border4 i.age_cat3 i.educ i.elect_dist if wave<=6
 
reg VOTE9 i.wave i.age_cat3 i.educ i.elect_dist if wave<=6 
reg VOTE9 i.wave##on_border4 i.age_cat3 i.educ i.elect_dist if wave<=6 


